jQuery(document).ready(function($) {

	if(!$('.upload_image_preview').attr('value')) {
		$('.upload_image_preview').hide();
	}

	$('.upload_image_button').click(function() {
		var el = $(this).parent();
		var uploader = wp.media({
			multiple: false,
			frame: 'select',
			library : {
				type : 'image'
			}
		})
		.on('select', function() {
			var selection = uploader.state().get('selection');
			var attachment = selection.first().toJSON();
			console.log(attachment);
			$('input', el).val(attachment.url);
			$('img', el).attr('src', attachment.sizes.thumbnail.url);
			$('img', el).show();
		})
		.open();
	});
});